package com.example.erhuo.api;

import android.util.Log;

import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class NetAPI {
    public static String URL = "http://59.110.224.92:8080/";
    public static OkHttpClient myHttpClient = new OkHttpClient();
    public static final MediaType JSONType = MediaType.get("application/json; charset=utf-8");

    public static void postJson(String url, RequestBody body, Callback callback) {
        //RequestBody body = RequestBody.create(json, JSONType);
        //Log.i("NetAPI",json);
        Request request = new Request.Builder()
                .url(URL + url)
                .post(body)
                .build();
        myHttpClient.newCall(request).enqueue(callback);
        Log.i("NetAPI", URL + url + " post done");
    }

    public static void postJsonWithToken(String url, RequestBody body, Callback callback, String token) {
        Request request = new Request.Builder()
                .header("token", token)
                .url(URL + url)
                .post(body)
                .build();
        myHttpClient.newCall(request).enqueue(callback);
        Log.i("NetAPI", URL + url + " post done");
    }

    public static void get(String url, Callback callback, String token) {
        Request request = new Request.Builder()
                .header("token", token)
                .url(URL + url)
                .get()
                .build();
        myHttpClient.newCall(request).enqueue(callback);
        Log.i("NetAPI", URL + url + " get done");
    }
}
